/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "pronounceconfig.h"
#include "src/platform.h"
#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include <QtGui/QApplication>
#include <QtGui/QStackedWidget>
#include <QtGui/QLabel>
#include <QtGui/QCheckBox>
#include <QtGui/QLineEdit>
#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>

//Defauls

const int
PronounceConfig::m_default_tts_type_index = PronounceConfig::GoogleTts,
PronounceConfig::m_default_tts_source_index = PronounceConfig::ResultText;

const QString
PronounceConfig::m_default_tts_command = Platform::getTts(),
PronounceConfig::m_defaut_play_command = Platform::getPlayer();

const bool
PronounceConfig::m_default_enble_auto_pronounce = false;


PronounceConfig::PronounceConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();    changeEvent(new QEvent(QEvent::LanguageChange));



    connect(ttsTypeComboBox, SIGNAL(currentIndexChanged(int)),ttsStackedWidget, SLOT(setCurrentIndex(int)));
    read();
}

PronounceConfig::~PronounceConfig() {
//    delete ttsStackedWidget;
//    delete ttsTypeLabel;
//    delete ttsCommandLabel;
//    delete playCommandLabel;
//    delete autoPronounceLabel;
//    delete enableAutoPronounce;
//    delete ttsCommandLine;
//    delete playCommandLine;
//    delete ttsTypeComboBox;
//    delete ttsSourceComboBox;
//    delete ttsPage;
//    delete playPage; << problem
//    delete ttsLayout; // <<Problem
//    delete playLayout;
//    delete ttsTypeLayout;
//    delete ttsLayout;
//    delete mainLayout;
}

void PronounceConfig::save() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Pronounce");
    s.setValue("tts", ttsTypeComboBox->currentIndex());
    s.setValue("ttsSource", ttsSourceComboBox->currentIndex());

    s.setValue("ttsCommand", ttsCommandLine->text());
    s.setValue("playCommand", playCommandLine->text());
    s.setValue("enableAutoPronounce", enableAutoPronounce->isChecked());
    s.endGroup();
}

void PronounceConfig::read() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Pronounce");
    ttsTypeComboBox->setCurrentIndex(s.value("tts", m_default_tts_type_index).toInt());
    ttsSourceComboBox->setCurrentIndex(s.value("ttsSource", m_default_tts_source_index).toInt());
    ttsCommandLine->setText(s.value("ttsCommand",m_default_tts_command).toString());
    playCommandLine->setText(s.value("playCommand", m_defaut_play_command).toString());
    enableAutoPronounce->setChecked(s.value("enableAutoPronounce", m_default_enble_auto_pronounce).toBool());
    s.endGroup();
}

void PronounceConfig::truncate() {
    ttsTypeComboBox->setCurrentIndex(m_default_tts_type_index);
    ttsSourceComboBox->setCurrentIndex(m_default_tts_source_index);
    ttsCommandLine->setText(m_default_tts_command);
    playCommandLine->setText(m_defaut_play_command);
    enableAutoPronounce->setChecked(m_default_enble_auto_pronounce);
}

QString PronounceConfig::getTtsCommand() const {
    return ttsCommandLine->text();
}

QString PronounceConfig::getPlayCommand() const {
    return playCommandLine->text();
}

PronounceConfig::Tts PronounceConfig::getCurrentTts() const {
    return (Tts)ttsTypeComboBox->itemData(ttsTypeComboBox->currentIndex()).toInt();
}

PronounceConfig::Target PronounceConfig::getCurrentTarget() const {
    return (Target)ttsSourceComboBox->itemData(ttsSourceComboBox->currentIndex()).toInt();
}

bool PronounceConfig::getAutoPronounceEnabled() const {
    return enableAutoPronounce->isChecked();
}

void PronounceConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	ttsTypeLabel->setText(QApplication::tr("TTS Type"));
	ttsCommandLabel->setText(QApplication::tr("TTs Command"));
	playCommandLabel->setText(QApplication::tr("Mp3 play command"));
	autoPronounceLabel->setText(QApplication::tr("Auto pronounce for"));

	enableAutoPronounce->setText(QApplication::tr("Enable Auto pronounce"));

	ttsTypeComboBox->clear();
	ttsTypeComboBox->addItem(QApplication::tr("Local tts"), GoogleTts);
	ttsTypeComboBox->addItem(QApplication::tr("Google Tts"), LocalTts);

	ttsSourceComboBox->clear();
	ttsSourceComboBox->addItem(QApplication::tr("Source text"), SourceText);
	ttsSourceComboBox->addItem(QApplication::tr("Result text"), ResultText);
    }
}

void PronounceConfig::createUi() {
    ttsStackedWidget = new QStackedWidget(this);

    ttsTypeLabel = new QLabel(this);
    ttsCommandLabel = new QLabel(this);
    playCommandLabel = new QLabel(this);
    autoPronounceLabel = new QLabel(this);

    enableAutoPronounce = new QCheckBox(this);

    ttsCommandLine = new QLineEdit(this);
    playCommandLine = new QLineEdit(this);

    ttsTypeComboBox = new QComboBox(this);
    ttsSourceComboBox = new QComboBox(this);

    ttsPage = new QWidget(this);
    playPage = new QWidget(this);

    ttsLayout = new QHBoxLayout;
    playLayout = new QHBoxLayout;
    ttsTypeLayout = new QHBoxLayout;
    ttsSourceLayout = new QHBoxLayout;

    mainLayout = new QVBoxLayout(this);




    ttsTypeLayout->addWidget(ttsTypeLabel);
    ttsTypeLayout->addWidget(ttsTypeComboBox);

    ttsLayout->addWidget(ttsCommandLabel);
    ttsLayout->addWidget(ttsCommandLine);

    playLayout->addWidget(playCommandLabel);
    playLayout->addWidget(playCommandLine);

    ttsPage->setLayout(ttsLayout);
    playPage->setLayout(playLayout);

    ttsStackedWidget->insertWidget(0, ttsPage);
    ttsStackedWidget->insertWidget(1, playPage);



    ttsSourceLayout->addWidget(autoPronounceLabel);
    ttsSourceLayout->addWidget(ttsSourceComboBox);

    mainLayout->addLayout(ttsTypeLayout);
    mainLayout->addWidget(ttsStackedWidget);
    mainLayout->addWidget(enableAutoPronounce);
    mainLayout->addLayout(ttsSourceLayout);
    mainLayout->addStretch();

    this->setLayout(mainLayout);
}
